﻿using System;
using System.Web.Mvc;
using System.Text;
using System.Web.Routing;

namespace Home.WebUI.Models.HtmlHelpers
{
    /// <summary>
    /// 分页帮助类
    /// </summary>
    public static class PagingHelpers
    {
        /// <summary>
        /// 返回分页
        /// </summary>
        /// <param name="html">此方法将扩展的 HTML 帮助器实例。</param>
        /// <param name="pagingInfo">分页信息</param>
        /// <param name="pageUrl">分页地址</param>
        ///<param name="displayPages">显示页数默认10</param>
        /// <returns></returns>
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl, int displayPages=10)
        {
            //参数
            var rq = System.Web.HttpContext.Current.Request.QueryString;
            StringBuilder canshu = new StringBuilder();
            foreach (var item in rq.AllKeys)
            {
                if (item!="Page")
                {
                    canshu.Append("&");
                    canshu.Append(item);
                    canshu.Append("=");
                    canshu.Append(rq[item]);
                }
            }
            if (rq != null && rq.Count > 0)
            { 
                
            }
            StringBuilder result = new StringBuilder();//返回的html
            int startpag = 1;//开始页码
            int endpag = pagingInfo.TotalPages;//结束页码
            int leftDisplayPages = (int)Math.Truncate((decimal)displayPages / 2);//当前页左边显示的最大页数
            int rightDisplayPages = displayPages - leftDisplayPages - 1;//当前页右边显示的最大页数
            //如果当前页大于左边显示页
            if (pagingInfo.CurrentPage > leftDisplayPages + 1)
            {
                //如果右侧能显示的页小于最大右边显示页
                if (pagingInfo.TotalPages - pagingInfo.CurrentPage < rightDisplayPages)
                {
                    //计算开始页
                    startpag = pagingInfo.CurrentPage - (displayPages - (pagingInfo.TotalPages - pagingInfo.CurrentPage) - 1);
                    if (startpag<1)
                    {
                        startpag = 1;
                    }
                }
                else
                {
                    //计算开始页
                    startpag = pagingInfo.CurrentPage - leftDisplayPages;
                    endpag = pagingInfo.CurrentPage + rightDisplayPages;
                }                
            }
            else//不大于则从1开始判断结束页
            {
                if (displayPages<pagingInfo.TotalPages)
                {
                    endpag = displayPages;
                }
            }
                   
            if (pagingInfo.CurrentPage>1)
            {
                string strPrev = pageUrl(pagingInfo.CurrentPage - 1);
                if (strPrev.IndexOf('?')<0)
                {
                    if (!string.IsNullOrEmpty(canshu.ToString()))
                    {
                        canshu.Replace('&', '?', 0, 1);
                    }                    
                }
                strPrev = strPrev + canshu.ToString();
                TagBuilder liPrev = new TagBuilder("li");
                TagBuilder tagPrev = new TagBuilder("a");//上一页
                tagPrev.MergeAttribute("href", strPrev);
                tagPrev.InnerHtml = "上一页";
                liPrev.InnerHtml = tagPrev.ToString();
                result.Append(liPrev.ToString());
            }
            for (int i = startpag; i <= endpag; i++)
            {
                string strPrev = pageUrl(i);
                if (strPrev.IndexOf('?') < 0)
                {
                    if (!string.IsNullOrEmpty(canshu.ToString()))
                    {
                        canshu.Replace('&', '?', 0, 1);
                    } 
                }
                strPrev = strPrev + canshu.ToString();
                TagBuilder li = new TagBuilder("li");     
                TagBuilder tag = new TagBuilder("a"); // 构建一个 <a> 标记
                tag.MergeAttribute("href", strPrev);
                tag.InnerHtml = i.ToString();   
                if (i == pagingInfo.CurrentPage)
                {
                    li.AddCssClass("current");
                    li.InnerHtml = i.ToString();
                }
                else
                {
                    li.InnerHtml = tag.ToString();
                }                
                result.Append(li.ToString());
            }
            if (pagingInfo.CurrentPage < pagingInfo.TotalPages)
            {
                string strPrev = pageUrl(pagingInfo.CurrentPage + 1);
                if (strPrev.IndexOf('?') < 0)
                {
                    if (!string.IsNullOrEmpty(canshu.ToString()))
                    {
                        canshu.Replace('&', '?', 0, 1);
                    } 
                }
                strPrev = strPrev + canshu.ToString();
                TagBuilder liNext = new TagBuilder("li");
                TagBuilder tagNext = new TagBuilder("a");//下一页
                tagNext.MergeAttribute("href", strPrev);
                tagNext.InnerHtml = "下一页";
                liNext.InnerHtml = tagNext.ToString();
                result.Append(liNext.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }

    }
}